//
//  CoachViewController.m
//  KiklinkApp
//
//  Created by LJC on 14-10-6.
//  Copyright (c) 2014年 Kiklink. All rights reserved.
//

#import "CoachViewController.h"

#import "ImagesManager.h"
#import "UIColor+hex.h"

#import "CoachCell.h"

#import "API.h"

static NSString *CoachTableCellIdentifier = @"CoachCell";

@interface CoachViewController ()
{
    //data
    NSMutableArray *coachTableArray;
}
@end

@implementation CoachViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"教练信息";
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:AppColor_BackGround];
    
    
    //loading
    [self.activity startAnimating];
    self.refreshBtn.hidden = YES;
    [self.refreshBtn addTarget:self action:@selector(refresh) forControlEvents:UIControlEventTouchUpInside];
    
    
    //BundleImages
    ImagesManager *imagesManager = [ImagesManager sharedImagesManager];
    
    
    //leftNavItem
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setFrame:CGRectMake(0, 0, NavBarHeight/4, NavBarHeight/2)];
    [backButton setBackgroundImage:[imagesManager back] forState:UIControlStateNormal];
    [backButton setBackgroundImage:[imagesManager backTapped] forState:UIControlStateHighlighted];
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftNavBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftNavBarItem;
    
    
    //coachTable
    self.coachTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.coachTable.backgroundColor = [UIColor colorWithHexString:AppColor_BackGround];
    self.coachTable.delegate = self;
    self.coachTable.dataSource = self;
    
    
    //data
    coachTableArray = [[NSMutableArray alloc] init];
    [self getData];
}

#pragma mark - get data
- (void)getData
{
    [API coachWithProductId:[NSNumber numberWithInteger:self.productID] Callback:^(int stateCode, NSString *word, id content) {
        
        if (stateCode == SUCCESS) {
            
            NSMutableArray *coaches= [NSMutableArray arrayWithArray:(NSArray *)content];
            [coachTableArray addObjectsFromArray:coaches];
            self.coachTable.scrollEnabled = YES;
            [self loadData];
            
        } else {
            
            self.coachTable.scrollEnabled = NO;
            [self.activity stopAnimating];
            self.refreshBtn.hidden = NO;
            
        }
        
    }];
}

- (void)loadData
{
    [self.coachTable reloadData];
    [self.activity stopAnimating];
}

- (void)refresh
{
    self.refreshBtn.hidden = YES;
    [self.activity startAnimating];
    
    [self getData];
}
#pragma mark - get data End

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - back
- (void)back
{
    self.coachTable.delegate = nil;
    self.coachTable.dataSource = nil;
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - back End

#pragma tableView dataSource&delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return coachTableArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BOOL nibsRegistered = NO;
    if (!nibsRegistered) {
        UINib *nib = [UINib nibWithNibName:@"CoachCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:CoachTableCellIdentifier];
        nibsRegistered = YES;
    }
    CoachCell *cell = (CoachCell *)[tableView dequeueReusableCellWithIdentifier:CoachTableCellIdentifier];
    [cell loadDataWithCoach:[coachTableArray objectAtIndex:indexPath.row]];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [CoachCell cellHeight:[coachTableArray objectAtIndex:indexPath.row]];
}
#pragma tableView dataSource&delegate End

@end
